let UserDao = require("../dao/userDao")
let UserTasksDao = require("../dao/userTasksDao")
let JsonMessage = require("../models/JsonMessage")
let EmailController = require("../controller/EmailController")
let UserPojo = require("../models/UserPojo")
module.exports = {

    cancelOneTask: function (req, res) {
    UserTasksDao.deleteUserTask(req.session.userid,req.body.taskid).then((results) => {
        if (results.affectedRows>0) {
            res.json(new JsonMessage("1112", results, "Cancel User Task Success"))
        } else {
            res.json(new JsonMessage("4103", null, "Cancel User Task Failure"))
        }
    })
},
    createTasksEmailNotifications: function (req, res) {
        let userModel = new UserPojo();
        userModel.id = req.body.id;
        userModel.password = req.body.password
        userModel.fammily_name = req.body.fammilyName
        userModel.address = req.body.address
        userModel.email = req.body.email
        userModel.signup_time = Math.round(new Date().getTime() / 1000)
        userModel.given_name = req.body.givenName
        userDao.insertUser(userModel, req, res);
    },
    selectTodayTasks: function (req, res) {
        UserTasksDao.selectUserTodayTasks(req.session.userid).then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1002", results, "Today User Tasks"))
            } else {
                res.json(new JsonMessage("1003", null, "No Today User Tasks"))
            }
        })
    },

    catUserAccount: function (req, res) {
        UserDao.selectUserByID(req.session.userid).then((results => {
            if (results.rowsAffected > 0) {
                res.json(new JsonMessage("1103", results, "User Account Information"))
            } else {
                res.json(new JsonMessage("4103", results, "No Such A User Information"))
            }

        }))
    },
    modifyUserAccount: function (req, res) {
        UserDao.updateUser(req.body).then((results => {
            if (results.affectedRows > 0) {
                res.json(new JsonMessage("1103", results, "Modify User Account Sucess"))
            } else {
                res.json(new JsonMessage("4103", results, "Modify User Account Failure"))
            }

        }))
    },
    registUser: function (req, res) {
        let randomID = Math.floor(Math.random()*800000+100000)
        let dateBigInt = Math.round(new Date().getTime()/1000)
        let userPojo = new UserPojo(randomID,req.body.fammily_name,req.body.given_name,dateBigInt,req.body.address,req.body.email,req.body.password)
        UserDao.insertUser(userPojo).then((results => {
            if (results.affectedRows > 0) {
                EmailController.sendMailFunction(req.body.email,`Your User ID:${randomID},Your Password: ${req.body.password}`)
                res.json(new JsonMessage("1103", {"randomID":randomID}, "Insert User Account Sucess"))
            } else {
                res.json(new JsonMessage("4103", null, "Insert User Account Failure"))
            }

        }))
    },

}